package top.arwtec.mcm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;
import top.arwtec.mcm.entity.UserInfo;
import top.arwtec.mcm.service.UserInfoService;

import java.util.List;

/**
 * @author liyin
 */
@RestController
@RequestMapping("user")
@RequiredArgsConstructor
public class UserController{
    private final UserInfoService userInfoService;

    @GetMapping("/{name}")
    public UserInfo getUserInfo(@PathVariable(value = "name") String name) {
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserInfo::getUsername,name).or().eq(UserInfo::getMail,name).or().eq(UserInfo::getPhone,name);
        UserInfo user = userInfoService.getUserInfo(wrapper);
        return user;
    }

    // hasRole('ADMIN')
    @GetMapping
    @PreAuthorize("hasRole('ADMIN')")
    public Mono<List<UserInfo>> getUserInfoList(UserInfo user) {
        LambdaQueryWrapper<UserInfo> lqw = Wrappers.lambdaQuery(user);
        return Mono.just(userInfoService.getUserInfoList(lqw));
    }

    @GetMapping("/{id}")
    public UserInfo getUserInfo(@PathVariable(value = "id") Long id) {
        return userInfoService.getById(id);
    }
    @PostMapping
    public boolean  saveUserInfo(UserInfo user) {
        return userInfoService.saveUserInfo(user);
    }

    @PutMapping
    public boolean updateUserInfo(UserInfo user) {
        return userInfoService.updateUserInfo(user);
    }

    @DeleteMapping
    public boolean deleteUserInfo(UserInfo user) {
        return userInfoService.removeById(user);
    }


}
